<?php

namespace app\api\controller;

use think\Controller;
use think\Request;
use think\Loader;
use app\common\utils\Redis;

// 公共接口
class Utility extends Controller
{


	public static $sms_template = [
		1 => 'login', 2 => 'register', 3 => 'modify_passwd',4=>'find_passwd'
	];
	    //手机验证码发送
	public function postSendVerifyCode(Request $request)
	{
		Loader::import('Redis.Redis');

		$phone = $request->post('phone');

		if (!check_valid_phone($phone)) {
			to_json_encode(400, '手机号格式不正确', ['status' => false]);

		}

		$type = $request->post('type', 1);

		if (!is_numeric($type) || !isset(self::$sms_template[$type])) {
			to_json_encode(400, '未识别类型', ['status' => false]);
		}

		$verify_code = rand(100000, 999999);


		$res = send_sms_verify_code(
			$phone,
			[
				'code' => $verify_code,
				'template' => self::$sms_template[$type]
			]
		);

		//file_put_contents('send_verify_code.txt',json_encode($res),FILE_APPEND);
		if (!$res) {
			to_json_encode(400, '发送验证码失败', ['status' => false]);
		}

		// 验证码有效时间为五分钟 
		$redis = Redis::instance();

		$redis->set(self::$sms_template[$type].'_'.$phone, $verify_code, 300);

		to_json_encode(200, 'success', ['status' => true]);
	}
	
}
?>